In [1]:
import sys
sys.path.append('../..')
import pyotc
In [2]:
from scipy import pi, sin, randn, exp, sinc, sqrt
from scipy import arange
from collections import OrderedDict
def gauss(x, x0, sig):
return 1 / (sqrt(2 * pi * sig**2)) * exp(-0.5 * ((x - x0) / sig)**2)
fs = 102400
t = arange(0, 1, 1/fs)
x = OrderedDict()
x['gauss'] = 0.1 * gauss(t, 0.5, 0.125)
x['diff gauss'] = 0.2 * (0.5 - t) / (sqrt(2 * pi) * 0.125**2) * gauss(t, 0.5, 0.125)
x['sinc'] = sinc((t - 0.5)/0.02)**2
x['white noise'] = 0.2 * randn(len(t))
x['sin x exp'] = sin(2 * pi * 10 * t) * exp(-t/0.2)
In [7]:
fig = pyotc.plt.figure()
for name, val in x.items():
pyotc.add_plot_to_figure(fig, t, val, fmt='-', linewidth=1, alpha=0.5,
xlabel='Time (s)', ylabel='X-signal (V)', label=name,
showLegend=True)
pyotc.plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
fig
Out[7]:
In [8]:
psd = OrderedDict()
for name, val in x.items():
psd[name] = pyotc.gen_PSD_from_time_series(val, fs, 1, name=name)
In [9]:
ax = None
for p in psd.values():
fig = p.plot_psd(axis=ax)
if ax is None:
ax = fig.axes[0]
pyotc.plt.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
ax.set_title('PSD')
fig
Out[9]:
In [10]:
for name, val in x.items():
print('{0:10s} \t {1:1.16f}'.format(name, val.var()))
In [11]:
for name, val in psd.items():
s = sum(val.get_psd())
print('{0:10s} \t {1:1.16f}'.format(name, s))
In [12]:
for name in x:
print('{0:10s} \t {1: 1.3e}'.format(name, x[name].var() - sum(psd[name].get_psd())))
In [ ]: